//
//  ShellWithdrawViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellWithdrawViewModel.h"

@interface ShellWithdrawViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end

@implementation ShellWithdrawViewModel

/**
 Convert 提提币限额
 */
- (void)getWithdrawLimit{
    
     @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:kWithdrawLimit params:@{@"coinCode":self.wallertModel.coinCode?:@""} success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
  
            [self.withdrawDictionary addEntriesFromDictionary:responseObject[kResultData]];
            [self.reloadSubject sendNext:@(4)];
        }
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}
/**
 Convert 提币记录
 @param  isFirst 是否是第一次
 */
- (void)getCoinRecord:(BOOL)isFirst{
    
    if (isFirst) {
           self.pageIndex = 1;
           [self.dataArray removeAllObjects];
       }
       
    NSDictionary *params = @{@"busiCode": @"withdraw",
                             @"coinCode":self.wallertModel.coinCode?:@""};
       
       NSString *url = [NSString stringWithFormat:@"%@/%ld",kQueryWalletDetail,(long)self.pageIndex];
       @weakify(self);
       [LZNetworkingManager lz_request:@"get" url:url params:params success:^(id  _Nullable responseObject) {
           @strongify(self);
           NSArray *dataArray = responseObject[kResultData];
           
           for (int i = 0; i < dataArray.count; i++) {
               ShellWallertModel *wallertModel = [ShellWallertModel mj_objectWithKeyValues:dataArray[i]];
               [self.dataArray addObject:wallertModel];
           }
           //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
           NSInteger totalCount = [responseObject[@"total"] integerValue];
           if(self.dataArray.count == totalCount && self.dataArray.count > 0){
               //没有更多数据
               [self.reloadSubject sendNext:@(0)];
               return;
           }
           //看是否有数据
           if(self.dataArray.count == 0){
               [self.reloadSubject sendNext:@(0)];
           } else {
               self.pageIndex++;
               [self.reloadSubject sendNext:@(1)];
           }
           
       } failure:^(NSError * _Nullable error) {
           
           [self.reloadSubject sendNext:@(0)];
           
       } notwork:^(NSError * _Nullable error) {
           [self.reloadSubject sendNext:@(0)];
           
       } isLoading:NO isFailTip:YES];

    
}
/**
 Convert 提现
 */
- (void)tradeNumber:(NSString *)number
            address:(NSString *)address
           password:(NSString *)password{
    

    NSString *coinCode = @"";
    switch (self.wallertModel.currencyType) {
        case ShellCurrencyTypeETH_USDT:{
          
            coinCode = @"ETH_USDT";
        }
            break;
        case ShellCurrencyTypeETH:{
            coinCode = @"ETH";
          
            
        }
            break;
        case ShellCurrencyTypeUSDT:{

            coinCode = @"USDT";

          }
              break;
        case ShellCurrencyTypeBTC:{

            coinCode = @"BTC";

            }
            break;
        case ShellCurrencyTypeShell:{

            coinCode = @"SHELL";

            }
            break;
        default:
            break;
    }
    
    
    
    
    
    
    
    //提现成功以后,不用返回到上一页, z还在当前页面 刷新提币记录
    NSDictionary *params = @{@"amount":number?:@"0",
                             @"address":address?:@"",
                             @"coinCode":coinCode,
                             @"payPassword":password?:@""};
   
    @weakify(self);
   ///质押成功. 应该返回上页面
   [LZNetworkingManager lz_request:@"post" url:kWithdraw params:params success:^(id  _Nullable responseObject) {
       if([responseObject[kResultStatus] isEqualToString:kCode]){
           @strongify(self);
           showDelayedDismissTitle(LZLocalizedString(@"申请提现成功"), nil);
           [self getCoinRecord:YES];

       }
   } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];

    
    
}
- (NSMutableDictionary *)withdrawDictionary{
    if(!_withdrawDictionary){
        _withdrawDictionary = [[NSMutableDictionary alloc] init];
    }
    return _withdrawDictionary;
}
@end
